<!DOCTYPE html>












  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
























<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2">

<link rel="stylesheet" href="/css/main.css?v=7.1.2">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.1.2">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=7.1.2">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=7.1.2">


  <link rel="mask-icon" href="/images/logo.svg?v=7.1.2" color="#222">







<script id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '7.1.2',
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false,"dimmer":false},
    back2top: true,
    back2top_sidebar: false,
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="在Node.js的官方下载网站上，有Linux Binaries(.tar.gz)的包可供下载，但是却没有说明文档。这里整理了Linux Binaries(.tar.gz)的一份安装说明。">
<meta name="keywords" content="node,安装">
<meta property="og:type" content="article">
<meta property="og:title" content="Linux下准备Node.js环境（Linux Binaries版）">
<meta property="og:url" content="http://blog.liangxh.cn/2015/11/05/prepare_node_environment/index.html">
<meta property="og:site_name" content="Liang Xinhui&#39;s Blog">
<meta property="og:description" content="在Node.js的官方下载网站上，有Linux Binaries(.tar.gz)的包可供下载，但是却没有说明文档。这里整理了Linux Binaries(.tar.gz)的一份安装说明。">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-06-26T06:53:59.311Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Linux下准备Node.js环境（Linux Binaries版）">
<meta name="twitter:description" content="在Node.js的官方下载网站上，有Linux Binaries(.tar.gz)的包可供下载，但是却没有说明文档。这里整理了Linux Binaries(.tar.gz)的一份安装说明。">





  
  
  <link rel="canonical" href="http://blog.liangxh.cn/2015/11/05/prepare_node_environment/">



<script id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>Linux下准备Node.js环境（Linux Binaries版） | Liang Xinhui's Blog</title>
  






  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?4bdb802b323fa2f04a51fca36789b479";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>







  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Liang Xinhui's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <h1 class="site-subtitle" itemprop="description">生活随笔 / 知识积累/ 技术交流</h1>
      
    
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="Toggle navigation bar">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">

    
    
    
      
    

    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>Home</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">

    
    
    
      
    

    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>Tags</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
    
      
    

    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>Archives</a>

  </li>

      
      
    </ul>
  

  

  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://blog.liangxh.cn/2015/11/05/prepare_node_environment/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="梁鑫辉">
      <meta itemprop="description" content="但行好事，莫问前程">
      <meta itemprop="image" content="/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Liang Xinhui's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">Linux下准备Node.js环境（Linux Binaries版）

              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              

              
                
              

              <time title="Created: 2015-11-05 08:27:00" itemprop="dateCreated datePublished" datetime="2015-11-05T08:27:00+08:00">2015-11-05</time>
            

            
          </span>

          

          
            
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>在Node.js的<a href="https://nodejs.org/en/download/" target="_blank" rel="noopener">官方下载网站</a>上，有<code>Linux Binaries(.tar.gz)</code>的包可供下载，但是却没有说明文档。<br>这里整理了<code>Linux Binaries(.tar.gz)</code>的一份安装说明。</p>
<a id="more"></a>
<p>以<code>Ubuntu</code>为例，其他Linux环境类似：</p>
<h1 id="目标"><a href="#目标" class="headerlink" title="目标"></a>目标</h1><ul>
<li>可运行Node.js</li>
<li>针对国内网络环境，添加<a href="http://npm.taobao.org/" target="_blank" rel="noopener">淘宝cnpm</a>的支持</li>
<li>添加<a href>nodemon</a>，以便调整代码后自动重启</li>
<li>添加开机自启动</li>
</ul>
<h1 id="基本环境部署"><a href="#基本环境部署" class="headerlink" title="基本环境部署"></a>基本环境部署</h1><h2 id="获取并解压Node-js二进制文件包"><a href="#获取并解压Node-js二进制文件包" class="headerlink" title="获取并解压Node.js二进制文件包"></a>获取并解压Node.js二进制文件包</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 进入安装包目标路径,根据个人喜好放置</span></span><br><span class="line"><span class="built_in">cd</span> /home/software</span><br><span class="line"><span class="comment"># 获取安装包</span></span><br><span class="line">wget https://nodejs.org/dist/v4.2.2/node-v4.2.2-linux-x64.tar.gz</span><br><span class="line"><span class="comment"># 解压</span></span><br><span class="line">tar xvzf node-v4.2.2-linux-x64.tar.gz</span><br></pre></td></tr></table></figure>

<h2 id="设置环境变量"><a href="#设置环境变量" class="headerlink" title="设置环境变量"></a>设置环境变量</h2><p>这样，并不能在系统任意位置执行<code>node</code>或者<code>npm</code>.
需要将路径添加到<code>PATH</code>中。<br>编辑<code>/etc/profile</code>，修改<code>PATH</code>环境变量：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">export</span> PATH=<span class="variable">$PATH</span>:/home/software/node-v4.2.2-linux-x64/bin</span><br></pre></td></tr></table></figure>

<p>这样，除了<code>node</code>主程序和自带的<code>npm</code>，每次通过<code>npm -g</code>安装的包，一般都能用非sudo访问了。</p>
<p>添加sudo对npm的访问：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo ln -s /home/software/node-v4.2.2-linux-x64/bin/npm /usr/<span class="built_in">local</span>/bin/</span><br></pre></td></tr></table></figure>

<h2 id="安装cnpm"><a href="#安装cnpm" class="headerlink" title="安装cnpm"></a>安装cnpm</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sudo npm install -g cnpm --registry=https://registry.npm.taobao.org</span><br><span class="line">sudo ln -s /home/software/node-v4.2.2-linux-x64/bin/npm /usr/<span class="built_in">local</span>/bin/</span><br></pre></td></tr></table></figure>

<p>之后就可以用cnpm代替npm进行包管理了，速度会快很多。</p>
<h2 id="安装nodemon"><a href="#安装nodemon" class="headerlink" title="安装nodemon"></a>安装nodemon</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo cnpm install -g nodemon</span><br></pre></td></tr></table></figure>

<p>由于已经设置过环境变量，此时可以直接运行<code>nodemon</code>了。</p>
<h1 id="自启动相关"><a href="#自启动相关" class="headerlink" title="自启动相关"></a>自启动相关</h1><p>一般会把自启动命令添加到<code>/etc/rc.local</code>中，这样，系统重启时就会运行。<br>但是之前在<code>/etc/profile</code>中设置的环境变量，在<code>/etc/rc.local</code>中并不起作用。<br>我的做法是，在<code>/usr/local/bin</code>中添加<code>node</code>、<code>nodemon</code>这些程序的<code>软链接</code>:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sudo ln -s /home/software/node-v4.2.2-linux-x64/bin/node /usr/<span class="built_in">local</span>/bin/</span><br><span class="line">sudo ln -s /home/software/node-v4.2.2-linux-x64/bin/nodemon /usr/<span class="built_in">local</span>/bin/</span><br></pre></td></tr></table></figure>

<p>然后，在<code>/etc/rc.local</code>中便可以设置开机自启动了：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 进入要启动程序的目录</span></span><br><span class="line"><span class="built_in">cd</span> /home/ubuntu/workspace/<span class="built_in">test</span></span><br><span class="line"><span class="comment"># 获取当前日期，格式为:20151105</span></span><br><span class="line">DATE=$(date +%Y%m%d)</span><br><span class="line"><span class="comment"># 启动程序，并将日志计入当天日期的文件：log_20151105.txt</span></span><br><span class="line">sudo nodemon app.js &gt;&gt; log_<span class="variable">$&#123;DATE&#125;</span>.txt &amp;</span><br><span class="line"><span class="comment"># 恢复执行目录</span></span><br><span class="line"><span class="built_in">cd</span> -</span><br></pre></td></tr></table></figure>
      
    </div>

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/node/" rel="tag"># node</a>
          
            <a href="/tags/安装/" rel="tag"># 安装</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2015/11/04/pack_python_to_exe/" rel="next" title="Python脚本打包成exe程序总结">
                <i class="fa fa-chevron-left"></i> Python脚本打包成exe程序总结
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2015/12/09/debug_library_in_netbeans/" rel="prev" title="通过NetBeans调试子项目">
                通过NetBeans调试子项目 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/uploads/avatar.jpg" alt="梁鑫辉">
            
              <p class="site-author-name" itemprop="name">梁鑫辉</p>
              <div class="site-description motion-element" itemprop="description">但行好事，莫问前程</div>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">13</span>
                    <span class="site-state-item-name">posts</span>
                  </a>
                </div>
              

              

              
                
                
                <div class="site-state-item site-state-tags">
                  
                    
                      <a href="/tags/">
                    
                  
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">27</span>
                    <span class="site-state-item-name">tags</span>
                  </a>
                </div>
              
            </nav>
          

          

          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="https://github.com/liangxinhui" title="GitHub &rarr; https://github.com/liangxinhui" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="mailto:liangxinhui@qq.com" title="E-Mail &rarr; mailto:liangxinhui@qq.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                </span>
              
            </div>
          

          

          
          

          
            
          
          

        </div>
      </div>

      
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
            
            
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#目标"><span class="nav-number">1.</span> <span class="nav-text">目标</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#基本环境部署"><span class="nav-number">2.</span> <span class="nav-text">基本环境部署</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#获取并解压Node-js二进制文件包"><span class="nav-number">2.1.</span> <span class="nav-text">获取并解压Node.js二进制文件包</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#设置环境变量"><span class="nav-number">2.2.</span> <span class="nav-text">设置环境变量</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#安装cnpm"><span class="nav-number">2.3.</span> <span class="nav-text">安装cnpm</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#安装nodemon"><span class="nav-number">2.4.</span> <span class="nav-text">安装nodemon</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#自启动相关"><span class="nav-number">3.</span> <span class="nav-text">自启动相关</span></a></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      

      

    </div>
  </aside>
  


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">  <a href="http://www.beian.miit.gov.cn" rel="noopener" target="_blank">豫ICP备15030440号-1 </a>&copy; 2015 – <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">梁鑫辉</span>

  

  
</div>









        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

    

    
  </div>

  

<script>
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  <script src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>


  


  <script src="/js/utils.js?v=7.1.2"></script>

  <script src="/js/motion.js?v=7.1.2"></script>



  
  


  <script src="/js/affix.js?v=7.1.2"></script>

  <script src="/js/schemes/pisces.js?v=7.1.2"></script>




  
  <script src="/js/scrollspy.js?v=7.1.2"></script>
<script src="/js/post-details.js?v=7.1.2"></script>



  


  <script src="/js/next-boot.js?v=7.1.2"></script>


  

  

  

  


  


  




  

  

  

  

  

  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>


  

  

  

  

  

  
<script>
  $('.highlight').not('.gist .highlight').each(function(i, e) {
    var $wrap = $('<div>').addClass('highlight-wrap');
    $(e).after($wrap);
    $wrap.append($('<button>').addClass('copy-btn').append('Copy').on('click', function(e) {
      var code = $(this).parent().find('.code').find('.line').map(function(i, e) {
        return $(e).text();
      }).toArray().join('\n');
      var ta = document.createElement('textarea');
      var yPosition = window.pageYOffset || document.documentElement.scrollTop;
      ta.style.top = yPosition + 'px'; // Prevent page scroll
      ta.style.position = 'absolute';
      ta.style.opacity = '0';
      ta.readOnly = true;
      ta.value = code;
      document.body.appendChild(ta);
      ta.select();
      ta.setSelectionRange(0, code.length);
      ta.readOnly = false;
      var result = document.execCommand('copy');
      
        if (result) $(this).text('Copied');
        else $(this).text('Copy failed');
      
      ta.blur(); // For iOS
      $(this).blur();
    })).on('mouseleave', function(e) {
      var $b = $(this).find('.copy-btn');
      setTimeout(function() {
        $b.text('Copy');
      }, 300);
    }).append(e);
  })
</script>


  

  

</body>
</html>
